package ocr.ocr.ui.util;

import android.util.Log;
import android.util.Pair;

import androidx.annotation.Nullable;

import com.zjw.zy.smi.Third;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/26 14:25
 */
public final class OcrTextUtils {

    private static final String TAG = "OcrTextUtils";
    private static final char EMPTY_CHAR = ' ';
    private static final int ENTER_LIMIT = 5;

    /**
     * 高精度 含位置版本文字识别
     */
    @Nullable
    public static Pair<String, Float> parseHighAndLocation(String result) {
        Log.d(TAG, "parseHighAndLocation() called with: result = [" + result + "]");
        Third<WordsItem[], Integer, Integer> parse = parseToWordsItem(result);
        if (parse == null) {
            return null;
        }
        WordsItem[] wordsItems = parse.a;
        int paperWidth = parse.b;
        int paperHeight = parse.c;

        //墙布
        char[] paper = new char[paperWidth * paperHeight];
        int currentPoint = 0;

        for (WordsItem wordsItem : wordsItems) {
            int nextStart = wordsItem.getTextStart() + wordsItem.getTextTop() * paperWidth;
            //填充空白
            while (currentPoint < nextStart) {
                paper[currentPoint++] = EMPTY_CHAR;
            }
            //移动画笔
            for (int i = 0; i < wordsItem.words.length; i++) {
                paper[currentPoint++] = wordsItem.words[i];
            }
        }

        //填充换行
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < paper.length; i++) {
            sb.append(paper[i]);
            if ((i + 1) % (paperWidth) == 0) {
                sb.append("\n");
            }
        }

        //最佳字体size
        float textSp = Math.min(Math.max(11f, 200.f / paperWidth), 15f);

        return Pair.create(sb.toString(), textSp);
    }

    @Nullable
    private static Third<WordsItem[], Integer, Integer> parseToWordsItem(String result) {
        try {
            //以字体size为准
            int paperWidth = 0;
            int paperHeight = 0;
            JSONObject root = new JSONObject(result);
            JSONArray rootArray = root.optJSONArray("words_result");
            if (rootArray == null) {
                return null;
            }
            WordsItem[] wordsArray = new WordsItem[rootArray.length()];
            for (int i = 0; i < rootArray.length(); i++) {
                JSONObject realData = rootArray.getJSONObject(i);
                String words = realData.optString("words");
                JSONObject location = realData.optJSONObject("location");
                WordsItem wordsItem = new WordsItem();
                wordsItem.words = words.toCharArray();
                if (location != null) {
                    wordsItem.height = location.optInt("height");
                    wordsItem.left = location.optInt("left");
                    wordsItem.top = location.optInt("top");
                    wordsItem.width = location.optInt("width");
                    paperWidth = Math.max(wordsItem.getCurrentLineWidth(), paperWidth);
                    paperHeight = Math.max(wordsItem.getCurrentLineHeight(), paperHeight);
                }
                wordsArray[i] = wordsItem;
            }

            return Third.create(wordsArray, paperWidth, paperHeight);
        } catch (Exception e) {
            Log.e(TAG, "parseToWordsItem: " + e.getMessage());
        }
        return null;
    }

    public static String parseBasicText(String result) {
        Third<WordsItem[], Integer, Integer> wordsItem = parseToWordsItem(result);

        if (wordsItem == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();

        for (WordsItem item : wordsItem.a) {
            sb.append(item.words);
            sb.append(" ");
        }
        return sb.toString();
    }


    private static class WordsItem {
        //字体高
        int height;
        //x坐标
        int left;
        //y坐标
        int top;
        //字宽
        int width;
        //内容
        char[] words = new char[]{};

        int getCurrentLineWidth() {
            double sizeW = words.length / (width * 1.0);
            return (int) ((left + width) * sizeW + 1);
        }

        int getCurrentLineHeight() {
            return (top + height) / height + 1;
        }

        /**
         * 文字的水平起点 包含文字尺寸
         */
        int getTextStart() {
            double sizeW = words.length / (width * 1.0);
            return (int) (left * sizeW + 1);
        }

        /**
         * 文字的垂直起点 包含文字尺寸
         */
        int getTextTop() {
            return top / height;
        }

    }
}
